package com.jstyle.jclife.view;

import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.ExerciseRecodeData;
import com.haibin.calendarview.MonthView;

/**
 *
 * Created by huanghaibin on 2017/11/15.
 */

public class EcgHistoryMonthView extends MonthView {

    private int mRadius;
    private Paint mRingPaint = new Paint();
    private int mRingRadius;
    private Context mContext;

    public EcgHistoryMonthView(Context context) {
        super(context);
        mContext = context;
        //兼容硬件加速无效的代码
        setLayerType(View.LAYER_TYPE_SOFTWARE, mSelectedPaint);
        //4.0以上硬件加速会导致无效
        mSelectedPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));

        setLayerType(View.LAYER_TYPE_SOFTWARE, mSchemePaint);
        //  mSchemePaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
        mSchemePaint.setStyle(Paint.Style.STROKE);
        mSchemePaint.setStrokeWidth(dipToPx(context, 1));
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(Color.parseColor("#264cef"));
        mRingPaint.setStyle(Paint.Style.FILL);
        mRingPaint.setStrokeWidth(dipToPx(context, 2));

        setLayerType(View.LAYER_TYPE_SOFTWARE, mRingPaint);
        // mRingPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));

    }

    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 6 * 2;
        mRingRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
        mSelectTextPaint.setTextSize(dipToPx(getContext(), 17));
    }

    /**
     * 如果需要点击Scheme没有效果，则return true
     *
     * @param canvas    canvas
     * @param calendar  日历日历calendar
     * @param x         日历Card x起点坐标
     * @param y         日历Card y起点坐标
     * @param hasScheme hasScheme 非标记的日期
     * @return false 则不绘制onDrawScheme，因为这里背景色是互斥的
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {

        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
       // canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
        Log.i(TAG, "onDrawSelected: "+calendar.getMonth()+" "+calendar.getDay());
        canvas.drawCircle(cx, cy, mRingRadius, mRingPaint);
        return !calendar.isCurrentDay();
    }

    private static final String TAG = "EcgHistoryMonthView";
    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;


        if(calendar.isCurrentDay()){
            mSchemePaint.setStyle(Paint.Style.FILL);
            mSchemePaint.setColor(Color.parseColor("#c8c7cc"));
        }else{
            mSchemePaint.setStyle(Paint.Style.STROKE);
        }
        canvas.drawCircle(cx, cy, mRingRadius, mSchemePaint);

    }

    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y - dipToPx(getContext(), 1);
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        Rect rect = new Rect();
        rect.left = cx - mItemWidth / 3;
        rect.right = cx + mItemWidth / 3;
        rect.top = cy - mItemHeight / 3;
        rect.bottom = cy + mItemHeight / 3;
//        if(calendar.isCurrentDay()){
//
//            mSchemePaint.setStyle(Paint.Style.FILL);
//            canvas.drawCircle(cx, cy, mRingRadius, mSchemePaint);
//        }
     //   canvas.drawRect(rect, mRingPaint);
        if (hasScheme) {
            mSchemeTextPaint.setColor(Color.WHITE);
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);

        } else {
            mCurMonthTextPaint.setColor(Color.WHITE);
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,

                    calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
        String date=calendar.getYear()+"."+String.format("%02d",calendar.getMonth())+"."+String.format("%02d",calendar.getDay());
        ExerciseRecodeData exerciseRecodeData=maps.get(date);
        if (exerciseRecodeData!=null) {
            mSchemePaint.setStyle(Paint.Style.STROKE);
            if(exerciseRecodeData.getEcg()!=ExerciseRecodeData.NoData) drawEcgData(canvas, x, y);
        }
    }
    private void drawEcgData(Canvas canvas, int x, int y) {
        mSchemePaint.setColor(Color.RED);
        int offsetTop = dipToPx(getContext(), 22);
        int offsetBottom = dipToPx(getContext(), 8);
        int height=mItemHeight - offsetBottom-offsetTop;
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        canvas.drawCircle(cx, cy, mRingRadius, mSchemePaint);

    }


    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    @SuppressWarnings("all")
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
